<template>
  <a-spin :spinning="confirmLoading">
    <a-row style="margin-bottom: 0;" v-if="model.canEdit">
      <a-col :span="24" style="text-align: right;padding-right:16px">
        <a-button v-has="'assetContractInfo:delete'" type="primary" @click="handleEdit">
          编辑
        </a-button>
        <a-popconfirm style="margin-left: 8px;" v-has="'assetContractInfo:edit'" placement="leftBottom" :title="'确定删除，合同' + this.model.contractName+'（' + this.model.contractCode+'）吗？'" @confirm="() => handleDelete()">
          <a-button type="danger" >
            删除
          </a-button>
        </a-popconfirm>
      </a-col>
    </a-row>
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="合同编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractCode">
              {{ model.contractCode || '-'}}
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractName">
              {{ model.contractName || '-'}}
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractType">
              {{ model.contractType_dictText || '-'}}
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="签约主体" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="signingSubject">
              {{ model.signingSubject || '-'}}
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同金额（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractAmount">
              {{ model.contractAmount || '-'}}
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractStartDate">
              {{ model.contractStartDate || '-'}}
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractEndDate">
              {{ model.contractEndDate || '-'}}
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractStatus">
              {{ model.contractStatus_dictText || '-'}}
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="付款日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentDate">
              {{ model.paymentDate || '-'}}
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachment">
              <j-upload-view v-model="model.attachment"   v-if="model.attachment"></j-upload-view>
              <span v-else>-</span>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="关联资产" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="assetId">
              {{ model.assetId_dictText || '-'}}
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              {{ model.remark || '-'}}
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-tabs default-active-key="1">
            <a-tab-pane key="1" tab="关联资产">
              <a-table
                ref="tableAsset"
                size="middle"
                :scroll="{x:true}"
                bordered
                rowKey="id"
                :columns="model.canEdit ? columnsAsset : columnsAssetDis"
                :dataSource="dataSourceAsset"
                :pagination="ipaginationAsset"
                class="j-table-force-nowrap"
                @change="assetTableChange">
                <span slot="action" slot-scope="text, record">
                  <a @click="handleDetail(record)">详情</a>
                </span>
              </a-table>
            </a-tab-pane>
          </a-tabs>
          <a-tabs default-active-key="1">
            <a-tab-pane key="1" tab="变更历史">
              <a-table
                ref="tableItem"
                size="middle"
                :scroll="{x:true}"
                bordered
                rowKey="id"
                :columns="columnsItem"
                :dataSource="dataSourceItem"
                :pagination="ipaginationItem"
                class="j-table-force-nowrap"
                @change="itemTableChange">
              </a-table>
            </a-tab-pane>
          </a-tabs>
        </a-row>
      </a-form-model>
    </j-form-container>
    <asset-message-view-modal ref="modalView3"></asset-message-view-modal>
  </a-spin>
</template>

<script>

  import { validateDuplicateValue } from '@/utils/util'
  import { deleteAction, getAction,downFile,httpAction } from '@/api/manage'
  import JUploadView from '@/components/jeecg/JUploadView'
    // import AssetMessageViewModal from './AssetMessageViewModal'

  export default {
    name: 'AssetContractInfoForm',
    components: {
      // AssetMessageViewModal
      JUploadView
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          contractName: [{required: true, message: '请输入合同名称!'}],
          contractCode: [{required: true, message: '请输入合同编号!'}],
        },
        url: {
          add: "/asset/assetContractInfo/add",
          edit: "/asset/assetContractInfo/edit",
          queryById: "/asset/assetContractInfo/queryById",
          delete: "/asset/assetContractInfo/delete",
          changeList: "/asset/assetMessageChange/list",
          assetList: "/asset/assetMessage/getByContractId"
        },
        columnsItem: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'操作人',
            align:"center",
            dataIndex: 'createBy_dictText'
          },
          {
            title:'操作时间',
            align:"center",
            dataIndex: 'createTime'
          },
          {
            title:'变更字段',
            align:"center",
            dataIndex: 'changeFieldName'
          },
          {
            title:'旧值',
            align:"center",
            dataIndex: 'oldValue'
          },
          {
            title:'新值',
            align:"center",
            dataIndex: 'newValue'
          }
        ],
        dataSourceItem:[],
        /* 分页参数 */
        ipaginationItem:{
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + "-" + range[1] + " 共" + total + "条"
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        },
        columnsAsset: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'资产编码',
            align:"center",
            dataIndex: 'assetCode'
          },
          {
            title:'资产名称',
            align:"center",
            dataIndex: 'assetName'
          },
          {
            title:'资产类型',
            align:"center",
            dataIndex: 'type_dictText'
          },
          {
            title:'资产地点',
            align:"center",
            dataIndex: 'place'
          },
          {
            title:'资产状态',
            align:"center",
            dataIndex: 'status_dictText'
          },
          {
            title:'负责人',
            align:"center",
            dataIndex: 'chargePerson'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:80,
            scopedSlots: { customRender: 'action' }
          }
        ],
        columnsAssetDis: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'资产编码',
            align:"center",
            dataIndex: 'assetCode'
          },
          {
            title:'资产名称',
            align:"center",
            dataIndex: 'assetName'
          },
          {
            title:'资产类型',
            align:"center",
            dataIndex: 'type_dictText'
          },
          {
            title:'资产地点',
            align:"center",
            dataIndex: 'place'
          },
          {
            title:'资产状态',
            align:"center",
            dataIndex: 'status_dictText'
          },
          {
            title:'负责人',
            align:"center",
            dataIndex: 'chargePerson'
          }
        ],
        dataSourceAsset:[],
        /* 分页参数 */
        ipaginationAsset:{
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + "-" + range[1] + " 共" + total + "条"
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        },
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.$options.components.AssetMessageViewModal = require('./AssetMessageViewModal').default
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.dataSourceItem = [];
        this.dataSourceAsset = [];
        this.loadItemList();
        this.loadAssetList();
        this.visible = true;
      },
      submitForm () {
      },
      handleDelete(){
        deleteAction(this.url.delete, {id: this.model.id}).then((res) => {
          if (res.success) {
            this.$message.success(res.message);
            this.$emit('ok');
          } else {
            this.$message.warning(res.message);
          }
        });
      },
      loadItemList(){
        var params = {pageNo: this.ipaginationItem.current, pageSize: this.ipaginationItem.pageSize, pid: this.model.id};
        // this.loadingItem = true;
        getAction(this.url.changeList, params).then((res) => {
          if (res.success) {
            this.dataSourceItem = res.result.records||res.result;
            if(res.result.total)
            {
              this.ipaginationItem.total = res.result.total;
            }else{
              this.ipaginationItem.total = 0;
            }
          }else{
            this.$message.warning(res.message)
          }
        }).finally(() => {
          // this.loadingItem = false
        })
      },
      itemTableChange(pagination, filters, sorter){
        this.ipaginationItem.current = pagination.current;
        this.ipaginationItem.pageSize = pagination.pageSize;
        this.loadItemList();
      },
      loadAssetList(){
        if(!this.model.assetId){
          this.dataSourceAsset = [];
          this.ipaginationAsset.total = 0;
          return;
        }
        var params = {pageNo: this.ipaginationAsset.current, pageSize: this.ipaginationAsset.pageSize, ids: this.model.assetId};
        // this.loadingItem = true;
        getAction(this.url.assetList, params).then((res) => {
          if (res.success) {
            this.dataSourceAsset = res.result.records||res.result;
            if(res.result.total)
            {
              this.ipaginationAsset.total = res.result.total;
            }else{
              this.ipaginationAsset.total = 0;
            }
          }else{
            this.$message.warning(res.message)
          }
        }).finally(() => {
          // this.loadingItem = false
        })
      },
      assetTableChange(pagination, filters, sorter){
        this.ipaginationAsset.current = pagination.current;
        this.ipaginationAsset.pageSize = pagination.pageSize;
        this.loadAssetList();
      },
      handleEdit(){
        this.$emit('edit', this.model);
      },
      handleDetail:function(record){
        this.$refs.modalView3.edit(record);
        this.$refs.modalView3.title="资产详情";
        this.$refs.modalView3.disableSubmit = true;
      },
    }
  }
</script>